package com.qf.service.impl;

import com.qf.dao.CartDao;
import com.qf.dao.impl.CartDaoImpl;
import com.qf.entity.Cart;
import com.qf.entity.Goods;
import com.qf.service.CartService;
import com.qf.service.GoodsService;

import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.service.impl
 * @description: ${TODO}
 * @date 2022/6/23 16:37
 */
public class CartServiceImpl implements CartService {
    private CartDao cartDao=new CartDaoImpl();
    @Override
    public void add(Cart cart) {
        cartDao.insert(cart);
    }

    @Override
    public Cart findByUidAndPid(Integer id, int pid) {
        return cartDao.selectByUidAndPid(id,pid);
    }

    @Override
    public void update(Cart cart) {
        cartDao.update(cart);
    }

    @Override
    public List<Cart> findByUid(Integer id) {
        List<Cart> cartList = cartDao.selectByUid(id);
        if (cartList != null) {
            GoodsService goodsService=new GoodsServiceImpl();
            //根据商品id查询商品
            for (Cart cart : cartList) {
                Goods goods=goodsService.findById(cart.getPid());
                cart.setGoods(goods);
            }
        }
        return cartList;
    }

    @Override
    public void delete(Integer uid, int pid) {
        cartDao.delete(uid,pid);
    }

    @Override
    public void deleteByUid(Integer uid) {
        cartDao.deleteByUid(uid);
    }
}
